﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class Unterteilen : BaseImageFilter
    {
        /// <summary>
        /// 
        /// </summary>
        public Unterteilen()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_unterteilen"></param>
        internal Unterteilen(Unterteilen _unterteilen)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
            return _bitmap;
        }

        #region ITwoSourceFilter Members

        /// <summary>
        /// Processes the specified _bitmap source.
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_bitmap2"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, UnsafeBitmap _bitmap2)
        {
            UnsafeBitmap bitmapSource = _bitmap1;
            UnsafeBitmap bitmapDest = _bitmap2;

            Color colorSource = Color.Transparent;
            Color colorDest = Color.Transparent;

            Color color = Color.Transparent;

            int nHeight = bitmapSource.Height;
            int nWidth = bitmapSource.Width;
            int x = 0; 
            int y = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    colorSource = bitmapSource.GetPixel(x, y);
                    colorDest = bitmapDest.GetPixel(x, y);
                    color = Color.FromArgb(colorDest.R / colorSource.R, colorDest.G / colorSource.G, colorDest.B / colorDest.B);
                    bitmapDest.SetPixel(x, y, color);
                }
            }
            bitmapSource.Dispose();
            return _bitmap2;
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Unterteilen(this);
        }
    }
}
